<?php
namespace Home\Controller;
use Home\Controller\CommonController;

/** 金蛋
     */
class MarketController extends CommonController{
     
    /*
     * 房地产分析模块首页
     * 1、展示已建立的数据模块
     * 2、新建数据模块
     */
    public function Index(){


        $obj=D('Eggs');
        $condition = 'status=1 and (isSystem=1 or uid='.(int)$_SESSION['userInfo']['id'].')';
        $data=$obj->field('id,ui,uid,createTime,hits,listType,sort')->where($condition)->order('sort asc')->select();
        foreach($data as  $key=>$val){
            $data[$key]['ui']=unserialize(str_replace('\\', '',$val['ui']));
        }
        
        $elist = M('eggList')->order('sort asc')->select();
        
        foreach($elist as $k=>$v){
            foreach($data as $keys=>$vals){
                if($v['id'] == $vals['listType']){
                    $elist[$k]['data'][]=$vals;
                }
            }
        }
        
        $this->assign('elist',$elist);
        $this->assign('data',$data);
        $this->display();
    }
    
    /*
     * 金蛋详情页
     */
    public function details($id){
        $id=(int)$id;
        if(empty($id)){
            $this->error('无效操作！');
        }
        
        $obj=D('Eggs');
        
        $obj->setCondition('and (uid='.$_SESSION['userInfo']['id'].' or isdefault=1)');
        
        $find=$obj->relation(true)->field('id,ui,uid,quickCondition,modelData,listType')->where(array('status'=>1,'id'=>$id))->find();
        
        $this->assign('listType',$find['listType']);
        
        //分析参数
        $param =  unserialize(str_replace('\\', '',$find['modelData']));

        $qCondition = unserialize(str_replace('\\', '',$find['quickCondition']));
    
        if(!empty($find['where'])){
            $conditions=array();
    
            foreach($find['where'] as $vals){                
                $con = unserialize(str_replace('\\', '',$vals['defaultConditions']));
        
                //对条件组进行去空处理
                $conditions = $this->dealArrayEmpty($con);
                
                //默认条件
                if($vals['isdefault']==1){
                    $param['where']=$conditions;
                }
                //条件组：用于查询条件时下拉选择
                $groupCondition[]=array(
                    'title'=>$vals['title'],
                    'condition'=>  json_encode($conditions)
                );
            }
        }else{
            $param['where'] = array();
            $groupCondition = array();
        }       
        //获取分析数据
        $param['code'] = empty($param['code'])?'Bdata':$param['code'];
        $isCache = 1;//使用缓存功能
        $data=$this->getEggCacheFile($param,$isCache);
        $getWhere = new \Common\Controller\BdataConfigController($param['code'], $isCache);
        
        $quickCondition=$getWhere->getConditionsData($qCondition);
        
        //$this->behavior($param);
        
        //图表及色阶参数
        $chart = C('ECHARTS');
        foreach ($data['c']['t'] as $key){
            $eChart[]=array('id'=>$key,'title'=>$chart[$key]);
        }

        $this->assign('eChart',$eChart);//图表
        $this->assign('colorLevel',$data['d']['c']['l']);//色阶参数
        $this->assign('find',unserialize(str_replace('\\', '',$find['ui'])));//模型的ID号
        $this->assign('id',$find['id']);//模型的ID号
        $this->assign('quickCondition',$quickCondition);//快速条件项及选择值
		
        $this->assign('defaultCondition',$param['where']);//默认条件项明细
    
        $this->assign('data',  urlencode(json_encode($data)));
        $this->assign('groupCondition',$groupCondition);
        $obj->save(array('id'=>$find['id'],'hits'=>array('exp','hits+1')));
        $this->display();
    }
    
    /**
     * @快速搜索
     * @param type $id
     */
    public function search($id){
        $obj=M('Eggs');
        $find=$obj->field('modelData')->where(array('status'=>1,'id'=>$id))->find();
        $param=  unserialize(str_replace('\\', '',$find['modelData']));
                
        $post=I('post.');
        unset($post['id']);
        $param['where'] = $post;
        
        //$AnalyzeData=new \Common\Controller\BdataResultController($param);
        $AnalyzeData=new \Common\Controller\ReturnResultController($param);
        $data=$AnalyzeData->getAnalyzeDataResults(); 
        //$this->behavior($param);
        $this->ajaxReturn($data);
    }
    
    /**
     * @删除
     * @param type $id
     */
    public function del($id){
        if(empty($id)){
            $this->ajaxReturn(false);
        }
        $obj=D('Eggs');
        if($obj->save(array('id'=>$id,'status'=>0))){
            $this->ajaxReturn(true);
        }else{
            $this->ajaxReturn(false);
        }
    }
    
    /**
     * 序列化
     */
    function xlh(){
        $data = array(
            'CJRQ'=>ARRAY('2015-01-01','2015-12-31'),
            'JZMJ'=>ARRAY('',''),
            'TNMJ'=>ARRAY('',''),
            'JMDJ'=>ARRAY('',''),
            'CJJE'=>ARRAY('',''),
        );
        echo serialize($data);
    }

    /**
     * 重命名
     */
    public function eggrename()
    {
        $params = I('post.');
        $id = $params['id'];
        $name = $params['rename'];

        $info = D('Eggs')->where(array('id'=>$id))->find();
        $ui = unserialize(str_replace('\\', '',$info['ui']));
        if(!empty($ui)){
            $ui['title'] = $name;
        }else{
            $this->ajaxReturn(array('status'=>'0','msg'=>'没有找到修改信息'));
        }
        $data['ui'] = serialize($ui);

        $re = D('Eggs')->where(array('id'=>$id))->save($data);

        if($re){
            $this->ajaxReturn(array('status'=>'1','msg'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('status'=>'0','msg'=>'修改失败'));
        }
    }

    /**
     * 删除模型
     */
    public function deleteegg()
    {
        $params = I('get.');
        $id = (int)$params['id'];

        $info = D('Eggs')->where(array('id'=>$id))->find();
        if(empty($info)){
            $this->ajaxReturn(array('status'=>'0','msg'=>'没有找到修改信息'));
        }

        if(D('Eggs')->where(array('id'=>$id))->delete() !== false){
            $this->ajaxReturn(array('status'=>'1','msg'=>'删除成功'));
        }else{
            $this->ajaxReturn(array('status'=>'0','msg'=>'未知错误,删除失败'));
        }
    }

    /**
     * 获取模型分类
     */
    public function geteggparentlist()
    {
        $params = I('get.');
        $id = intval($params['id']);
        //当前的所属分类
        $title = D('Eggs')->alias('e')->join('__EGG_LIST__ l ON e.listType=l.id')->where(array('e.id'=>$id))->field('l.title')->find();

        $typelist = D('Egg_list')->order('sort')->select();

        $result['title'] = $title['title'];
        $result['typelist'] = $typelist;

        $this->ajaxReturn($result);
    }

    /**
     * 移动模型
     */
    public function removegg()
    {
        $params = I('get.');
        $id = intval($params['id']);
        $newtypeid = $params['newtypeid'];

        $oldtypeid = D('Eggs')->where(array('id'=>$id))->getField('listType');
        if($oldtypeid == $newtypeid){
            $this->ajaxReturn(array('status'=>'0','msg'=>'类型没有改变'));
        }
        $change = D('Eggs')->where(array('id'=>$id))->setField('listType',$newtypeid);
        if($change){
            $this->ajaxReturn(array('status'=>'1','msg'=>'移动成功'));
        }else{
            $this->ajaxReturn(array('status'=>'0','msg'=>'未知错误,修改失败'));
        }
    }
}
